package cc.wman.data.common.command;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Objects;

/**
 * Copyright (C),big-data
 * FileName: CommandEnum
 * Author:   maokai
 * Date:     2020/2/20 10:09
 * Description:
 */
public enum CommandEnum {
    ONE("1", "cc.wman.data.common.command.type.OneCommand"),
    TWO("2", "cc.wman.data.common.command.type.TwoCommand"),
    THIRD("3", "cc.wman.data.common.command.type.ThirdCommand"),
    FOUR("4", "cc.wman.data.common.command.type.FourCommand");
    private String value;
    private String clazz;

    CommandEnum(String value, String clazz) {
        this.value = value;
        this.clazz = clazz;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    public static InnerCommand getCommandInstance(String command) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        InnerCommand innerCommand = null;
        for (CommandEnum commandEnum : values()) {
            String value = commandEnum.getValue();
            if (Objects.equals(value, command)) {
                String clazz = commandEnum.getClazz();
                Class<?> aClass = Class.forName(clazz);
                Constructor<?> declaredConstructor = aClass.getDeclaredConstructor();
                innerCommand = (InnerCommand) declaredConstructor.newInstance();
                return innerCommand;
            }

        }
        return innerCommand;
    }
}
